package com.jt.www.remote;

import com.jt.www.domain.dto.CommissionPushDTO;
import com.jt.www.domain.qo.BrokersInfoQO;
import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.net.UnknownHostException;

@FeignClient(value = "jtsaleapp-svc", url = "${remote.jtsaleapp_url}",
        configuration = FeignConfiguration.class,
        fallbackFactory = JtsaleappClient.FeignClientFallbackFactory.class)
public interface JtsaleappClient {

    public static final Logger log = LoggerFactory.getLogger(JtsaleappClient.class);

    @PostMapping(value = "/a/channel/basiclaw/queryPrivateBrokers", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    String queryPrivateBrokers(@RequestBody BrokersInfoQO brokersInfoQO) throws Exception;

    @PostMapping(value = "/a/channel/basiclaw/channelPolicyToSaleSystem",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    String pushSale(@RequestBody CommissionPushDTO commissionPushDTO) throws Exception;

    @Component
    class FeignClientFallbackFactory implements FallbackFactory<JtsaleappClient> {

        @Override
        public JtsaleappClient create(Throwable cause) {
            return new JtsaleappClient() {

                @Override
                public String queryPrivateBrokers(BrokersInfoQO brokersInfoQO) {
                    log.error("全面营销私保人员信息接口查询失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public String pushSale(CommissionPushDTO commissionPushDTO) throws UnknownHostException {
                    log.error("{}-全面营销渠道保单推送私保接口调用失败,fallback reason:", "search", cause);
                    return null;
                }
            };
        }


    }
}
